class BorrowingsController < ApplicationController
  before_filter :librarian_authorize
  
  # GET /borrowings
  # GET /borrowings.json
  def index
    @borrowings = Borrowing.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @borrowings }
    end
  end

  # GET /borrowings/1
  # GET /borrowings/1.json
  def show
    @borrowing = Borrowing.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @borrowing }
    end
  end

  # GET /borrowings/new
  # GET /borrowings/new.json
  def new
    @borrowing = Borrowing.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @borrowing }
    end
  end

  # GET /borrowings/1/edit
  def edit
    @borrowing = Borrowing.find(params[:id])
  end

  # POST /borrowings
  # POST /borrowings.json
  def create
    @borrowing = Borrowing.new(params[:borrowing])
    @borrowing.borrow_date = Date.current
    @document = Document.find_by_isbn(@borrowing.document_id)
    @borrowing.document_name = @document.name
    if @document.current_number <= 0
      respond_to do |format|
        format.html { redirect_to @borrowing, notice: 'This document number is not enough to borrow' }
        #format.html { render action: "new" }
        #format.json { render json: @borrowing.errors, status: ':unprocessable_entity '}
      end
    else
      @document.current_number -= 1
      @document.save
  
      respond_to do |format|
        if @borrowing.save
          format.html { redirect_to @borrowing, notice: 'Borrowing was successfully created.' }
          format.json { render json: @borrowing, status: :created, location: @borrowing }
        else
          format.html { render action: "new" }
          format.json { render json: @borrowing.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  # PUT /borrowings/1
  # PUT /borrowings/1.json
  def update
    @borrowing = Borrowing.find(params[:id])

    respond_to do |format|
      if @borrowing.update_attributes(params[:borrowing])
        format.html { redirect_to @borrowing, notice: 'Borrowing was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @borrowing.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /borrowings/1
  # DELETE /borrowings/1.json
  def destroy
    @borrowing = Borrowing.find(params[:id])
    @borrowing.destroy

    respond_to do |format|
      format.html { redirect_to borrowings_url }
      format.json { head :no_content }
    end
  end
  
  def destroy1
    @borrowing = Borrowing.find(params[:id])
    if @borrowing
      @borrowing.return_date = Date.current
      @borrowing.save
      @document = Document.find_by_isbn(params[:document_id])
      @document.current_number += 1;
      @document.save
      respond_to do |format|
          format.html { redirect_to librarian_url, notice: 'Returning was successfully' }
      end
    else
      respond_to do |format|
          format.html { redirect_to librarian_url, notice: 'Fail' }
      end
    end
 
  end
  
end
